<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://www.w3.org/1999/xhtml" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xi="http://www.w3.org/2001/XInclude"
	xmlns:fr="http://orbeon.org/oxf/xml/form-runner">
	<xhtml:head>
		<title>Numishare: Edit Page</title>
		<!-- Core + Skin CSS -->
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/numishare/css/jquery-ui-1.8.10.custom.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/reset-fonts-grids.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/base-min.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/fonts-min.css"/>
		
		<!-- numishare styling -->
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/xforms.css"/>
		
		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/jquery-1.6.1.min.js"/>
		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/numishare-menu.js"/>
		<xforms:model>
			<!-- exist URL is stored in an XML file -->
			<xforms:instance id="exist-url">
				<xi:include href="exist-url.xml"/>
			</xforms:instance>
			
			<xforms:instance id="page">
				<page stub="" id="" xmlns="">
					<title/>
					<short-title/>
					<public>0</public>
					<text/>
				</page>
			</xforms:instance>
			
			<xforms:instance id="control-instance">
				<controls xmlns="">
					<collection-name/>
				</controls>
			</xforms:instance>
			
			<xforms:instance id="dump">
				<dump/>
			</xforms:instance>
			
			<!-- collections/authentication instances -->
			<xforms:instance id="collections-list">
				<collections xmlns="">
					<collection roll="admin" name="numishare"/>
				</collections>
			</xforms:instance>

			<xforms:instance id="config">
				<config xmlns=""/>
			</xforms:instance>

			<xforms:instance id="status">
				<status/>
			</xforms:instance>

			<xforms:instance id="identifier">
				<id xmlns=""/>
			</xforms:instance>

			<xforms:action ev:event="xforms-ready">
				<xxforms:variable name="mode" select="xxforms:get-request-parameter('mode')"/>
				<xforms:action if="$mode = 'edit'">
					<xforms:setvalue ref="instance('identifier')" value="xxforms:get-request-parameter('id')"/>
					<xforms:toggle case="edit-page" ev:event="xforms-model-construct-done"/>
				</xforms:action>
			</xforms:action>
			
			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:insert context="instance('control-instance')" nodeset="collection-name" position="after"
					origin="xxforms:call-xpl('oxf:/apps/numishare/xpl/get-authentication.xpl', 'dump', instance('dump'), 'data')"/>
				<xforms:send submission="load-collections"/>
			</xforms:action>
			
			<xforms:bind nodeset="instance('page')">
				<xforms:bind nodeset="@stub" required="true()"/>
				<xforms:bind nodeset="short-title" required="true">
					<xforms:bind constraint="string-length(.) &lt; 24"/>
				</xforms:bind>
				<xforms:bind nodeset="title" required="true()"/>
			</xforms:bind>
			<xforms:bind nodeset="instance('config')">
				<xforms:bind nodeset="pages">
					<xforms:bind nodeset="page">
						<xforms:bind nodeset="@stub" required="true()"/>
						<xforms:bind nodeset="short-title" required="true">
							<xforms:bind constraint="string-length(.) &lt; 24"/>
						</xforms:bind>
						<xforms:bind nodeset="title" required="true()"/>
					</xforms:bind>
				</xforms:bind>
			</xforms:bind>
			
			<xforms:submission id="load-collections" serialization="none" method="get" action="{instance('exist-url')}collections-list.xml" replace="instance" instance="collections-list"
				xxforms:username="admin" xxforms:password="">
				<!-- if the config loads successfully, set the collection names based on authentication -->
				<xforms:action ev:event="xforms-submit-done">
					<!-- set default if security is false -->
					<xforms:action if="not(string(instance('control-instance')/request-security/role))">
						<xforms:setvalue ref="instance('control-instance')/collection-name">numishare</xforms:setvalue>
					</xforms:action>
					<!-- if there is a security role, set the collection-name value if it is in the list, otherwise set new collection name -->
					<xforms:action if="string(instance('control-instance')/request-security/role)">
						<xforms:action if="string(instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name)">
							<xforms:setvalue ref="instance('control-instance')/collection-name"
								value="instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name"/>
						</xforms:action>
					</xforms:action>				
					<xforms:send submission="load-config"/>
				</xforms:action>
				<!-- if the config has not been created (given that the URL in exist-url.xml is correct), create it -->
				<xforms:message ev:event="xforms-submit-error" level="modal">Unable to load Numishare collections list.</xforms:message>
			</xforms:submission>

			<!-- Load File -->
			<xforms:submission id="load-config" serialization="none" method="get" action="{instance('exist-url')}{instance('control-instance')/collection-name}/config.xml" xxforms:username="admin" xxforms:password=""
				replace="instance" instance="config"/>

			<!-- Save File -->
			<xforms:submission id="save-config" ref="instance('config')" xxforms:username="admin" xxforms:password="" action="{instance('exist-url')}{instance('control-instance')/collection-name}/config.xml" method="put"
				replace="none">
				<xforms:setvalue ev:event="xforms-submit-done" ref="instance('status')">Page saved.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Save Error!</xforms:message>
			</xforms:submission>

			<xforms:action ev:event="xforms-value-changed" ev:observer="page-title">
				<xforms:setvalue ref="instance('page')/@stub" value="replace(instance('page')/title, ' ', '-')"/>
				<xforms:setvalue ref="instance('page')/@stub" value="encode-for-uri(lower-case(instance('page')/@stub))"/>
			</xforms:action>
			<xforms:action ev:event="xforms-value-changed" ev:observer="edit-page-title">
				<xforms:setvalue ref="instance('config')//page[@id=instance('identifier')]/@stub" value="replace(instance('config')//page[@id=instance('identifier')]/title, ' ', '-')"/>
				<xforms:setvalue ref="instance('config')//page[@id=instance('identifier')]/@stub" value="encode-for-uri(lower-case(instance('config')//page[@id=instance('identifier')]/@stub))"/>
			</xforms:action>
		</xforms:model>

	</xhtml:head>
	
	<xhtml:body class="yui-skin-sam">
		<xhtml:div id="doc4" class="yui-t2 content ui-corner-all">
			<xxforms:variable name="display_path">../../</xxforms:variable>
			<xi:include href="header.xml"/>			
			<div id="bd">
				<xhtml:div id="form">
					<xforms:group ref="instance('status')/text()">
						<div class="success">
							<xforms:output ref="instance('status')"/>
						</div>
					</xforms:group>
					<p>
						<a href="../../pages/">&lt; Return</a>
					</p>
					<xforms:switch>
						<xforms:case id="create-page">
							<xforms:group ref="instance('page')">
								<div class="submission">
									<xforms:trigger appearance="minimal">
										<xforms:label class="file_control"><img src="{$display_path}images/save.gif" alt="Save"/> Save</xforms:label>
										<xforms:action ev:event="DOMActivate">
											<xforms:setvalue ref="@id" value="string(digest( string( random( true ) ), 'MD5', 'hex' ))"/>
											<xforms:insert context="instance('config')/pages" nodeset="./child::node()[last()]" origin="instance('page')"/>
											<xforms:send submission="save-config"/>
										</xforms:action>
									</xforms:trigger>
								</div>
								<div class="section">
									<div>
										<xforms:input ref="title" id="page-title">
											<xforms:label>Title</xforms:label>
											<xforms:alert>Required.</xforms:alert>
										</xforms:input>
									</div>
									<div>
										<xforms:input ref="short-title">
											<xforms:label>Short Title</xforms:label>
											<xforms:alert>Required. 24 char max.</xforms:alert>
											<xforms:hint>Text that appears for header link.</xforms:hint>
										</xforms:input>
									</div>
									<div>
										<xforms:input ref="@stub">
											<xforms:label>Stub</xforms:label>
											<xforms:alert>Required.</xforms:alert>
										</xforms:input>
									</div>
									<div>
										<h3>Page Content</h3>
										<xforms:textarea ref="text" mediatype="text/html"/>
									</div>
								</div>
							</xforms:group>
						</xforms:case>
						<xforms:case id="edit-page">
							<div class="submission">
								<xforms:submit submission="save-config" appearance="minimal">
									<xforms:label class="file_control"><img src="{$display_path}images/save.gif" alt="Save"/> Save</xforms:label>
								</xforms:submit>
							</div>
							<xforms:group ref="instance('config')//page[@id=instance('identifier')]">
								<div class="section">
									<div>
										<xforms:input ref="title" id="edit-page-title">
											<xforms:label>Title</xforms:label>
											<xforms:alert>Required.</xforms:alert>
										</xforms:input>
									</div>
									<div>
										<xforms:input ref="short-title">
											<xforms:label>Short Title</xforms:label>
											<xforms:alert>Required. 24 char max.</xforms:alert>
											<xforms:hint>Text that appears for header link.</xforms:hint>
										</xforms:input>
									</div>
									<div>
										<xforms:input ref="@stub">
											<xforms:label>Stub</xforms:label>
											<xforms:alert>Required.</xforms:alert>
										</xforms:input>
									</div>
									<div>
										<h3>Page Content</h3>
										<xforms:textarea ref="text" mediatype="text/html"/>
									</div>
								</div>
							</xforms:group>
						</xforms:case>
					</xforms:switch>
					<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector" xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
				</xhtml:div>
			</div>
			
			<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector" xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
			
		</xhtml:div>		
	</xhtml:body>		
</html>